var game={
	date:[],
	score:0,  
	randomNum:function(){
		if(!this.isFull()){
			while(true){
				var row=Math.floor(Math.random()*4);
				var col=Math.floor(Math.random()*4);
				if(this.date[row][col]==0){
					this.date[row][col]=Math.random()<0.5?2:4;
					break;
				}
			}
		}
	},
	isFull:function(){
		return !(/(,0)|(0,)/).test(this.date.toString());
	},
	start:function(){
		this.date=[[0,0,0,0],
						 [0,0,0,0],
						 [0,0,0,0],
						 [0,0,0,0]
		];
		this.score=0;
		this.randomNum();
		this.randomNum();
		this.updateView();
	},
	updateView:function(){
		var span=document.getElementById("score");
		span.innerHTML=this.score;
		for(var row=0;row<this.date.length;row++){
			for(var col=0;col<this.date[row].length;col++){
				var div=document.getElementById("c"+row+col);
				if(this.date[row][col]!=0){
					var zhi=this.date[row][col]==2?"屌丝":
								this.date[row][col]==4?"实习生":
								this.date[row][col]==8?"程序员":
								this.date[row][col]==16?"小包工":
								this.date[row][col]==32?"大包工":
								this.date[row][col]==64?"主管":
								this.date[row][col]==128?"经理":
								this.date[row][col]==256?"总经理":
								this.date[row][col]==512?"CEO":
								this.date[row][col]==1024?"总裁":
								this.date[row][col]==2048?"董事长":
								this.date[row][col]==4096?"人生巅峰":
																		"此生无憾";
					div.innerHTML=zhi;

					div.className="cell n"+this.date[row][col];
				}else{
					div.innerHTML="";
					div.className="cell";
				}
			}
		}
		this.gameOver();
		var overDiv=document.getElementById("gameOver");
		var overDiv2=document.getElementById("gameOver2");
		if(this.state==this.GAMEOVER){
			var finalScore=document.getElementById("finalScore");
			finalScore.innerHTML=this.score;
			overDiv.style.display="block";
			var p=overDiv.getElementsByTagName("p")[0];
			p.style.left=(innerWidth-300)/2+"px";
			p.style.top=(innerHeight-200)/2+"px";
		}else if(this.state==this.GOOD){
			var finalScore=document.querySelector("#gameOver2 span");
			finalScore.innerHTML=this.score;
			var p=overDiv2.getElementsByTagName("p")[0];
			p.style.left=(innerWidth-300)/2+"px";
			p.style.top=(innerHeight-200)/2+"px";
			overDiv2.style.display="block";
		}else{
			overDiv.style.display="none";
			overDiv2.style.display="none";
		}
	},
	moveLeft:function(){
		var start=this.date.toString();
		for(var row=0;row<this.date.length;row++){
			var c=0;
			var col=1;
			while(col<4){
				var prev=this.date[row][col-1];
				var curr=this.date[row][col];
				if(curr!=0){
					this.merge(row,col,row,col-1);
				}
				if(curr!=0&&prev==0&&(col!=c+1)){
					col--;
				}else{
					col++;	
					if(curr!=0&&prev!=0&&this.date[row][c]!=0){
						c++;
					}
				}
			}
		}
		var end=this.date.toString();
		if(end!=start){
			this.randomNum();
			this.updateView();
		}
	},
	moveUp:function(){
		var start=this.date.toString();
		for(var col=0;col<4;col++){
			var r=0;
			var row=1;
			while(row<4){
				var prev=this.date[row-1][col];
				var curr=this.date[row][col];
				if(curr!=0){
					this.merge(row,col,row-1,col);
				}
				if(curr!=0&&prev==0&&(row!=r+1)){
					row--;
				}else{
					row++;
					if(curr!=0&&prev!=0&&this.date[r][col]!=0){
						r++;
					}
				}
			}
		}
		var end=this.date.toString();
		if(end!=start){
			this.randomNum();
			this.updateView();
		}
	},
	moveRight:function(){
		var start=this.date.toString();
		for(var row=0;row<4;row++){
			var c=3;
			var col=2;
			while(col>=0){
				var prev=this.date[row][col+1];
				var curr=this.date[row][col];
				if(curr!=0){
					this.merge(row,col,row,col+1);
				}
				if(curr!=0&&prev==0&&(col!=c-1)){
					col++;
				}else{
					col--;
					if(curr!=0&&prev!=0&&this.date[row][c]!=0){
						c--;
					}
				}
			}
		}
		var end=this.date.toString();
		if(end!=start){
			this.randomNum();
			this.updateView();
		}
	},
	moveDown:function(){
		var start=this.date.toString();
		for(var col=0;col<4;col++){
			var r=3;
			var row=2;
			while(row>=0){
				var prev=this.date[row+1][col];
				var curr=this.date[row][col];
				if(curr!=0){
					this.merge(row,col,row+1,col);
				}
				if(curr!=0&&prev==0&&(row!=r-1)){
					row++;
				}else{
					row--;
					if(curr!=0&&prev!=0&&this.date[r][col]!=0){
						r--;
					}
				}
			}
		}
		var end=this.date.toString();
		if(end!=start){
			this.randomNum();
			this.updateView();
		}
	},
	merge:function(row,col,prevrow,prevcol){
		if(this.date[prevrow][prevcol]==0){
			this.date[prevrow][prevcol]=this.date[row][col];
			this.date[row][col]=0;
		}else if(this.date[prevrow][prevcol]==this.date[row][col]){
			this.date[prevrow][prevcol]*=2;
			this.score+=this.date[prevrow][prevcol];
			this.date[row][col]=0;
		}
	},
	state:1,
	PLAYING:1,
	GAMEOVER:0,
	GOOD:2,
	gameOver:function(){
		if(this.has8192()){
			this.state=this.GOOD;
		}else if(this.isFull()&&!this.canMove()){
			this.state=this.GAMEOVER;
		}else{
			this.state=this.PLAYING;
		}
	},
	canMove:function(){
		for(var row=0;row<4;row++){
			for(var col=0;col<4;col++){
				var curr=this.date[row][col];
				if(col!=0){
					if(curr==this.date[row][col-1]){
						return true;
					}
				}
				if(col!=3){
					if(curr==this.date[row][col+1]){
						return true;
					}
				}
				if(row!=0){
					if(curr==this.date[row-1][col]){
						return true;
					}
				}
				if(row!=3){
					if(curr==this.date[row+1][col]){
						return true;
					}
				}
			}
		}
		return false;
	},
	has8192:function(){
		return this.date.toString().search(/8192/)!=-1;
	}
};

window.onload=function(){
	game.start();	
	document.onkeydown=function(event){
		if(event.preventDefault){  //如果DOM的取消默认行为函数存在，是真,则取消，否则用IE的取消函数 
			event.preventDefault();
		}else{ 
			event.returnValue=false;  //IE
		}//取消键盘事件的默认行为
		if(game.state==game.PLAYING){
			var code=event.which||event.keyCode||event.charCode;
			if(code==37){
				game.moveLeft();
			}else if(code==38){
				game.moveUp();
			}else if(code==39){
				game.moveRight();
			}else if(code==40){
				game.moveDown();
			}
		}
	}
}